#import "layer_menu_chapter.h"
#import "ReviewManager.h"

#define MENU_CHAPTER_BOOK_1_STAR_NEED 11
#define MENU_CHAPTER_BOOK_2_STAR_NEED 22

#define MENU_CHAPTER_FONT_TITILE @"font_book1.fnt"
#define MENU_CHAPTER_FONT_CONTEXT @"font_book.fnt"

#if (HARD_WARE == HARD_WARE_IPHONE)
	#define MENU_CHAPTER_TEXT_STAR_SIZE 18
#elif (HARD_WARE == HARD_WARE_IPAD)
	#define MENU_CHAPTER_TEXT_STAR_SIZE 32
#endif

#define TIME_MOVE 0.3
#define TIME_FADE_OUT 0.3
#define TIME_FADE_IN 0.3
#define TIME_PAGE 0.3

#define AUTO_MOVE_OFFSET 0.05
#define CLOUD_MOVE_SPEED 10

@implementation LayerMenuChapter

- (id)initLayerMenuChapter
{
	int i, j, k;
	int mission_count;
	int mission_id;

	SBook _Book;

	CCLabelTTF *_StarText;
	CCSprite *_Star;
	float _TextWidth;
	float _TextStartX;

	SPage _Page;
	NSString *_Path;
	CCLabelBMFont *_Label;
	SContentsPageIndex _PageIndex;

	if ((self = [super init]))
	{
		/*load resource
		*/
		reLoadResource(&g_CfgMenuChapterResourceTable);

		self.isTouchEnabled = true;

		/*back ground
		*/
		background_ = uiCreateImageObject(&g_CfgMenuChapterImageBackground, self);
		background_.scaleX = 2.2f;
		background_.scaleY = 2.0f;
		background_.anchorPoint = CGPointZero;
		background_.positionInPixels = CGPointZero;
		backgroundMoveRatio_ = (background_.contentSizeInPixels.width * 2 - g_ScrrenSize.width) / g_ScrrenSize.width;

		/*book
		*/
		for (i = 0; i < g_CfgMenuChapterBookTable.size(); i++)
		{
			_Book.pages.clear();

			/*book
			*/
			_Path = [NSString stringWithUTF8String:g_CfgMenuChapterBookTable[i].image_path];
			_Book.index = i;
			_Book.image = [[CCSprite alloc] initWithSpriteFrameName:_Path];
			[self addChild:_Book.image];
			[_Book.image release];
			_Book.image.positionInPixels = g_CfgMenuChapterBookTable[i].center_rect.center;
			switch (i)
			{
			case 1:
				if (MENU_CHAPTER_BOOK_1_STAR_NEED > RecrodGetTotalStarCount())
				{
					_Book.image.color = ccGRAY;
				}
				break;
			case 2:
				if (MENU_CHAPTER_BOOK_2_STAR_NEED > RecrodGetTotalStarCount())
				{
					_Book.image.color = ccGRAY;
				}
				break;
			default:;
			}

			/*star and text
			*/
			switch (i)
			{
			case 0:
				_StarText = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d/15", RecordGetChapterStarCount(0)] fontName:@"font_treasure.ttf" fontSize:MENU_CHAPTER_TEXT_STAR_SIZE];
				[_Book.image addChild:_StarText];
				_StarText.anchorPoint = ccp(0.0f, 0.5f);
				_StarText.color = ccBLACK;
				break;
			case 1:
				if (MENU_CHAPTER_BOOK_1_STAR_NEED > RecrodGetTotalStarCount())
				{
					_StarText = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"need %d", MENU_CHAPTER_BOOK_1_STAR_NEED - RecrodGetTotalStarCount()] fontName:@"font_treasure.ttf" fontSize:MENU_CHAPTER_TEXT_STAR_SIZE];
				}
				else
				{
					_StarText = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d/15", RecordGetChapterStarCount(1)] fontName:@"font_treasure.ttf" fontSize:MENU_CHAPTER_TEXT_STAR_SIZE];
				}
				[_Book.image addChild:_StarText];
				_StarText.anchorPoint = ccp(0.0f, 0.5f);
				_StarText.color = ccBLACK;
				break;
			case 2:
				if (MENU_CHAPTER_BOOK_2_STAR_NEED > RecrodGetTotalStarCount())
				{
					_StarText = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"need %d", MENU_CHAPTER_BOOK_2_STAR_NEED - RecrodGetTotalStarCount()] fontName:@"font_treasure.ttf" fontSize:MENU_CHAPTER_TEXT_STAR_SIZE];
				}
				else
				{
					_StarText = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d/15", RecordGetChapterStarCount(2)] fontName:@"font_treasure.ttf" fontSize:MENU_CHAPTER_TEXT_STAR_SIZE];
				}
				[_Book.image addChild:_StarText];
				_StarText.anchorPoint = ccp(0.0f, 0.5f);
				_StarText.color = ccBLACK;
				break;
			default:;
			}
			_Star = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgMenuChapterStarOn]];
			[_Book.image addChild:_Star];
			_Star.anchorPoint = ccp(0.0f, 1.0f);

			_TextWidth = _StarText.contentSizeInPixels.width + _Star.contentSizeInPixels.width;
			_TextStartX = _Book.image.contentSizeInPixels.width / 2.0f - _TextWidth / 2.0f;
			_StarText.positionInPixels = ccp(_TextStartX, _Star.positionInPixels.y - _Star.contentSizeInPixels.height / 2.0f);
			_Star.positionInPixels = ccp(_TextStartX + _StarText.contentSizeInPixels.width + 5, 0.0f);

			/*book page
			*/
			mission_count = g_CfgMenuChapterBookTable[i].missions.size();
			for (j = -1; j < mission_count; j++)
			{
				/*update mission id
				*/
				mission_id = g_CfgMenuChapterBookTable[i].missions[j];

				_Page.label.clear();

				/*image
				*/
				_Path = [NSString stringWithUTF8String:g_CfgMenuChapterPageBack];
				_Page.image = [CCSprite spriteWithSpriteFrameName:_Path];
				_Page.image.positionInPixels = g_ScrrenCenter;
				[self addChild:_Page.image];

				/*dummy
				*/
				_Page.dummy = [[CCSprite alloc] init];
				[_Page.image addChild:_Page.dummy];
				[_Page.dummy release];

				/*flower
				*/
				_Path = [NSString stringWithUTF8String:g_CfgMenuChapterPageFlower];
				_Page.flower = [CCSprite spriteWithSpriteFrameName:_Path];
				_Page.flower.positionInPixels = ccp(g_ScrrenSize.width / 2, 900);
				[_Page.dummy addChild:_Page.flower];

				/*label
				*/
				if (j == -1)
				{
					for (k = 0; k < mission_count + 1; k++)
					{
						if (0 == k)
						{
							_Label = [self CreateTitleText:@"CONTEXT"];
							_Label.positionInPixels = ccp(g_ScrrenCenter.x, 820 - k * 100);
						}
						else
						{
							_Label = [self CreateTitleText:[NSString stringWithFormat:@"*%s", g_CfgMenuChapterBookTable[i].titile[k - 1].c_str()]];
							_Label.positionInPixels = ccp(g_ScrrenCenter.x, 820 - k * 100);

							/*page index
							*/
							memset(&_PageIndex, 0, sizeof(SContentsPageIndex));
							_PageIndex.rect.center = _Label.positionInPixels;
							_PageIndex.rect.size = _Label.contentSizeInPixels;
							_PageIndex.index = k;
							_Page.page_index_table.push_back(_PageIndex);
						}
						[_Page.dummy addChild:_Label];
						_Page.label.push_back(_Label);
					}

					/*close book
					*/
					_Label = [self CreateContextText:[NSString stringWithFormat:@"Close The Book"]];
					_Label.positionInPixels = g_CfgMenuChapterLabelClosePosition;
					_Page.label.push_back(_Label);
					[_Page.dummy addChild:_Label];
					closeBookRect_.size = _Label.contentSizeInPixels;
				}
				else
				{
					/*title
					*/
					_Label = [self CreateTitleText:[NSString stringWithFormat:@"%s", g_CfgMenuChapterBookTable[i].titile[j].c_str()]];
					_Label.positionInPixels = g_CfgMenuChapterLabelTitilePosition;
					_Page.label.push_back(_Label);
					[_Page.dummy addChild:_Label];

					/*star
					*/
					_Path = [NSString stringWithUTF8String:g_CfgMenuChapterStarBackground];
					_Page.star_background = [CCSprite spriteWithSpriteFrameName:_Path];
					_Page.star_background.positionInPixels = g_CfgMenuChapterStarBackgroundPosition;
					/*star heart*/
					if (g_RecordMission[mission_id].flag_star_heart)
					{
						_Path = [NSString stringWithUTF8String:g_CfgMenuChapterStarOn];
					}
					else
					{
						_Path = [NSString stringWithUTF8String:g_CfgMenuChapterStarOff];
					}
					_Page.star[0] = [CCSprite spriteWithSpriteFrameName:_Path];
					/*star enemy*/
					if (g_RecordMission[mission_id].flag_star_enemy)
					{
						_Path = [NSString stringWithUTF8String:g_CfgMenuChapterStarOn];
					}
					else
					{
						_Path = [NSString stringWithUTF8String:g_CfgMenuChapterStarOff];
					}
					_Page.star[1] = [CCSprite spriteWithSpriteFrameName:_Path];
					/*star special*/
					if (g_RecordMission[mission_id].flag_star_special)
					{
						_Path = [NSString stringWithUTF8String:g_CfgMenuChapterStarOn];
					}
					else
					{
						_Path = [NSString stringWithUTF8String:g_CfgMenuChapterStarOff];
					}
					_Page.star[2] = [CCSprite spriteWithSpriteFrameName:_Path];
					_Page.star[0].positionInPixels = g_CfgMenuChapterStar1Position;
					_Page.star[1].positionInPixels = g_CfgMenuChapterStar2Position;
					_Page.star[2].positionInPixels = g_CfgMenuChapterStar3Position;
					[_Page.dummy addChild:_Page.star_background];
					[_Page.dummy addChild:_Page.star[0]];
					[_Page.dummy addChild:_Page.star[1]];
					[_Page.dummy addChild:_Page.star[2]];

					/*read book
					*/
					_Label = [self CreateContextText:[NSString stringWithFormat:@"Read!"]];
					_Label.positionInPixels = g_CfgMenuChapterLabelReadPosition;
					if (g_RecordMissionLock[mission_id])
					{
						[_Label runAction:[CCHide action]];
					}
					_Page.label.push_back(_Label);
					[_Page.dummy addChild:_Label];
					readBookRect_.size = _Label.contentSizeInPixels;

					/*close book
					*/
					_Label = [self CreateContextText:[NSString stringWithFormat:@"Close The Book"]];
					_Label.positionInPixels = g_CfgMenuChapterLabelClosePosition;
					_Page.label.push_back(_Label);
					[_Page.dummy addChild:_Label];
					closeBookRect_.size = _Label.contentSizeInPixels;
				}
				[_Page.image runAction:[CCHide action]];
				_Book.pages.push_back(_Page);
			}

			bookTable_.push_back(_Book);
		}

		readBookRect_.center = g_CfgMenuChapterLabelReadPosition;
		closeBookRect_.center = g_CfgMenuChapterLabelClosePosition;

		/*menu
		*/
		itemBack_ = CREATE_BTN_BACK(CallBackItemBack);
		itemBack_.anchorPoint = ccp(0.9f, 0.0f);
		itemBack_.positionInPixels = ccp(g_ScrrenSize.width, 0.0f);
		uiNodeAddMenu(self, CGPointZero, itemBack_, nil);

		/*flag
		*/
		state_ = CHAPTER_STATE_BOOK;
		flagTouchLock_ = false;
		flagFunctionLock_ = false;
		flagDrag_ = false;
		flagSelectBookPage_ = false;
		flagTurnToPage_ = false;
		turnToPageIndex_ = -1;
	}
	return self;
}

- (void)dealloc
{
	/*release resource
	*/
	reFreeResource(&g_CfgMenuChapterResourceTable);

	[super dealloc];
}

- (void)ccTouchesBegan:(NSSet *)Touch withEvent:(UIEvent *)Event
{
	CGPoint _Point;
	UITouch *_Touch;

	if (flagTouchLock_)
	{
		return;
	}

	_Touch = [Touch anyObject];
	_Point = [[CCDirector sharedDirector] convertToGL:[_Touch locationInView:[_Touch view]]];
	_Point = ConvertPositionToPositionInPixels(_Point);

	flagDrag_ = false;
	start_ = _Point;
	drag_ = _Point;
	end_ = _Point;
}

- (void)ccTouchesMoved:(NSSet *)Touch withEvent:(UIEvent *)Event
{
	CGPoint _Point;
	UITouch *_Touch;

	if (flagTouchLock_)
	{
		return;
	}

	_Touch = [Touch anyObject];
	_Point = [[CCDirector sharedDirector] convertToGL:[_Touch locationInView:[_Touch view]]];
	_Point = ConvertPositionToPositionInPixels(_Point);

	switch (state_)
	{
	case CHAPTER_STATE_BOOK:
		end_ = _Point;
		if (!flagDrag_)
		{
			if (10 < fabs(end_.x - start_.x))
			{
				flagDrag_ = true;
			}
		}
		else
		{
			[self BooksMove:end_.x - drag_.x];
			drag_ = end_;
		}
		break;
	case CHAPTER_STATE_PAGE:
		end_ = _Point;
		if (end_.x - start_.x < -100)
		{
			[self ActionNextPage];
		}
		else if (end_.x - start_.x > 100)
		{
			[self ActionPrevPage];
		}
		break;
	}
}

- (void)ccTouchesEnded:(NSSet *)Touch withEvent:(UIEvent *)Event
{
	int i;
	int _MissionID;
	CGPoint _Point;
	UITouch *_Touch;
	UICenterRect _CenterRect;

	if (flagTouchLock_)
	{
		return;
	}

	_Touch = [Touch anyObject];
	_Point = [[CCDirector sharedDirector] convertToGL:[_Touch locationInView:[_Touch view]]];
	_Point = ConvertPositionToPositionInPixels(_Point);

	switch (state_)
	{
	case CHAPTER_STATE_BOOK:
		if (!flagDrag_)
		{
			for (i = 0; i < bookTable_.size(); i++)
			{
				_CenterRect.center = bookTable_[i].image.positionInPixels;
				_CenterRect.size = g_CfgMenuChapterBookTable[bookTable_[i].index].center_rect.size;
				if (uiPointInCenterRect(_Point, _CenterRect))
				{
					/*check book unlock
					*/
					switch (bookTable_[i].index)
					{
					case 1:
						if (MENU_CHAPTER_BOOK_1_STAR_NEED > RecrodGetTotalStarCount())
						{
							return;
						}
						break;
					case 2:
						if (MENU_CHAPTER_BOOK_2_STAR_NEED > RecrodGetTotalStarCount())
						{
							return;
						}
						break;
					default:;
					}

					[self ActionSelectBook:bookTable_[i].index];
					break;
				}
			}
		}
		break;
	case CHAPTER_STATE_PAGE:
		if (ptPointDistance_CGPoint(start_, end_) < 10)
		{
			if (bookTable_[selectBookIndex_].active_page_index == 0)
			{
				for (i = 0; i < bookTable_[selectBookIndex_].pages[0].page_index_table.size(); i++)
				{
					if (uiPointInCenterRect(_Point, bookTable_[selectBookIndex_].pages[0].page_index_table[i].rect))
					{
						flagTouchLock_ = true;
						[self ActionTurnToPage:bookTable_[selectBookIndex_].pages[0].page_index_table[i].index];
						return;
					}
				}
				if (uiPointInCenterRect(_Point, closeBookRect_))
				{
					flagTouchLock_ = true;
					[self ActionCloseBook];
				}
			}
			else if (uiPointInCenterRect(_Point, readBookRect_))
			{
				_MissionID = g_CfgMenuChapterBookTable[selectBookIndex_].missions[bookTable_[selectBookIndex_].active_page_index - 1];

				/*check mission lock
				*/
				if (g_RecordMissionLock[_MissionID])
				{
					return;
				}

				flagTouchLock_ = true;
				g_ChpaterComicPrev = true;
				g_ChapterMissionID = _MissionID;
				[g_SceneMenu SceneChangeToComic];
			}
			else if (uiPointInCenterRect(_Point, closeBookRect_))
			{
				flagTouchLock_ = true;
				[self ActionCloseBook];
			}
		}
		break;
	}
}

- (CCLabelBMFont *)CreateTitleText:(NSString *)String
{
	CCLabelBMFont *_Text;

	_Text = [CCLabelBMFont labelWithString:String fntFile:MENU_CHAPTER_FONT_TITILE];
	return _Text;
}

- (CCLabelBMFont *)CreateContextText:(NSString *)String
{
	CCLabelBMFont *_Text;

	_Text = [CCLabelBMFont labelWithString:String fntFile:MENU_CHAPTER_FONT_CONTEXT];
	_Text.scale = 1.1f;
	return _Text;
}

- (void)SelectMission:(int)MissionID
{
	int i, j;
	int _BookIndex, _PageIndex;

	for (i = 0; i < g_CfgMenuChapterBookTable.size(); i++)
	{
		for (j = 0; j < g_CfgMenuChapterBookTable[i].missions.size(); j++)
		{
			if (MissionID == g_CfgMenuChapterBookTable[i].missions[j])
			{
				_BookIndex = i;
				_PageIndex = j + 1;
				break;
			}
		}
	}
	[self ActionSelectBookPage:_BookIndex And:_PageIndex];
}

- (void)BooksMove:(float)Offset
{
	int i;

	if (Offset < 0)
	{
		if (bookTable_[bookTable_.size() - 1].image.positionInPixels.x == g_ScrrenSize.width / 2)
		{
			return;
		}
		if (bookTable_[bookTable_.size() - 1].image.positionInPixels.x + Offset < g_ScrrenSize.width / 2)
		{
			Offset = g_ScrrenSize.width / 2 - bookTable_[bookTable_.size() - 1].image.positionInPixels.x;
		}
	}
	else
	{
		if (bookTable_[0].image.positionInPixels.x == g_ScrrenSize.width / 2)
		{
			return;
		}
		if (bookTable_[0].image.positionInPixels.x + Offset > g_ScrrenSize.width / 2)
		{
			Offset = g_ScrrenSize.width / 2 - bookTable_[0].image.positionInPixels.x;
		}
	}
	for (i = 0; i < bookTable_.size(); i++)
	{
		bookTable_[i].image.positionInPixels = ccp(bookTable_[i].image.positionInPixels.x + Offset, bookTable_[i].image.positionInPixels.y);
	}
	background_.positionInPixels = ccp(background_.positionInPixels.x + Offset * backgroundMoveRatio_, background_.positionInPixels.y);
}

- (void)ActionSelectBook:(int)Index
{
	int i, j;
	CCSequence *_Seq;
	id _ActionMove;
	id _ActionAniOpenFront;
	id _ActionAniOpenNext;
	CCCallFuncN *_CallBackOpenOver;

	flagTouchLock_ = true;
	selectBookIndex_ = Index;
	for (i = 0; i < bookTable_.size(); i++)
	{
		bookTable_[i].book_position = bookTable_[i].image.positionInPixels;
		if (bookTable_[i].index == Index)
		{
			_ActionMove = [action_move_in_pixel actionWithDuration:TIME_MOVE position:ccp(g_ScrrenSize.width / 2, g_ScrrenSize.height / 2)];
			_ActionAniOpenFront = [CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgMenuChapterBookTable[bookTable_[i].index].ani_open)];
			_ActionAniOpenNext = [CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgMenuChapterBookOpenNextAni)];
			_CallBackOpenOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionOpenOver:)];
			_Seq = [CCSequence actions:_ActionMove, _ActionAniOpenFront, _ActionAniOpenNext, _CallBackOpenOver, nil];
			[bookTable_[i].image runAction:_Seq];
		}
		else
		{
			_Seq = [CCSequence actions:[CCFadeOut actionWithDuration:TIME_FADE_OUT], [CCHide action], nil];
			[bookTable_[i].image runAction:_Seq];
		}
	}

	for (i = 0; i < bookTable_[selectBookIndex_].pages.size(); i++)
	{
		[bookTable_[selectBookIndex_].pages[i].image runAction:[[CCPageTurn3D actionWithSize:ccg(64, 96) duration:0] reverse]];
		bookTable_[selectBookIndex_].pages[i].dummy.opacity = 255;
		for (j = 0; j < bookTable_[selectBookIndex_].pages[i].label.size(); j++)
		{
			bookTable_[selectBookIndex_].pages[i].label[j].opacity = 255;
		}
	}

	/*hide item back
	*/
	flagFunctionLock_ = true;
	[itemBack_ runAction:[action_move_in_pixel actionWithDuration:BTN_MOVE_TIME position:ccp(g_ScrrenSize.width + itemBack_.contentSizeInPixels.width, 0.0f)]];
}

- (void)ActionCloseBook
{
	int i;
	int _ActivePage;
	CCSequence *_Seq;
	id _ActionFadeOut;
	CCCallFuncN *_CallBackFadeOutOver;

	flagTouchLock_ = true;
	_ActivePage = bookTable_[selectBookIndex_].active_page_index;
	_ActionFadeOut = [CCFadeOut actionWithDuration:TIME_FADE_OUT];
	_CallBackFadeOutOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionPageFadeOutOver:)];
	_Seq = [CCSequence actions:_ActionFadeOut, _CallBackFadeOutOver, nil];
	[bookTable_[selectBookIndex_].pages[_ActivePage].dummy runAction:_Seq];
	for (i = 0; i < bookTable_[selectBookIndex_].pages[_ActivePage].label.size(); i++)
	{
		_ActionFadeOut = [CCFadeOut actionWithDuration:0.5f];
		[bookTable_[selectBookIndex_].pages[_ActivePage].label[i] runAction:_ActionFadeOut];
	}
}

- (void)ActionNextPage
{
	CCSprite *_Cur;
	CCSprite *_Next;
	CCSequence *_Seq;
	id _Action;
	CCCallFuncN *_CallBack;

	if (bookTable_[selectBookIndex_].active_page_index < g_CfgMenuChapterBookTable[selectBookIndex_].missions.size())
	{
		flagTouchLock_ = true;
		_Cur = bookTable_[selectBookIndex_].pages[bookTable_[selectBookIndex_].active_page_index].image;
		_Next = bookTable_[selectBookIndex_].pages[bookTable_[selectBookIndex_].active_page_index + 1].image;
		[self reorderChild:_Cur z:2];
		[self reorderChild:_Next z:1];
		[_Cur runAction:[CCShow action]];
		[_Next runAction:[CCShow action]];
		_Action = [CCPageTurn3D actionWithSize:ccg(5, 5) duration:TIME_PAGE];
		_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionNextPageOver:)];
		_Seq = [CCSequence actions:_Action, _CallBack, nil];
		[_Cur runAction:_Seq];
	}
}

- (void)ActionPrevPage
{
	CCSprite *_Cur;
	CCSprite *_Prev;
	CCSequence *_Seq;
	id _Action;
	CCCallFuncN *_CallBack;

	if (bookTable_[selectBookIndex_].active_page_index > 0)
	{
		flagTouchLock_ = true;
		_Cur = bookTable_[selectBookIndex_].pages[bookTable_[selectBookIndex_].active_page_index].image;
		_Prev = bookTable_[selectBookIndex_].pages[bookTable_[selectBookIndex_].active_page_index - 1].image;
		[self reorderChild:_Cur z:1];
		[self reorderChild:_Prev z:2];
		[_Cur runAction:[CCShow action]];
		[_Prev runAction:[CCShow action]];
		_Action = [[CCPageTurn3D actionWithSize:ccg(5, 5) duration:TIME_PAGE] reverse];
		_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionPrevPageOver:)];
		_Seq = [CCSequence actions:_Action, _CallBack, nil];
		[_Prev runAction:_Seq];
	}
}

- (void)ActionSelectBookPage:(int)BookIndex And:(int)PageIndex
{
	flagSelectBookPage_ = true;
	turnToPageIndex_ = PageIndex;
	[self ActionSelectBook:BookIndex];
}

- (void)ActionTurnToPage:(int)Index
{
	flagTurnToPage_ = true;
	turnToPageIndex_ = Index;
	[self ActionNextPage];
}

- (void)CallBackItemBack:(id)Sender
{
	CCSequence *_Seq;
	id _Action;
	CCCallFuncN *_CallBack;

	if (flagFunctionLock_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	switch (state_)
	{
	case CHAPTER_STATE_BOOK:
		flagTouchLock_ = true;
		flagFunctionLock_ = true;
		_Action = [action_move_in_pixel actionWithDuration:BTN_MOVE_TIME position:ccp(g_ScrrenSize.width + itemBack_.contentSizeInPixels.width, 0.0f)];
		_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackItemBackActionOver:)];
		_Seq = [CCSequence actions:_Action, _CallBack, nil];
		[itemBack_ runAction:_Seq];
		break;
	case CHAPTER_STATE_PAGE: break;
	default:;
	}
}

- (void)CallBackItemBackActionOver:(id)Sender
{
	[g_SceneMenu SetState:SCENE_MENU_STATE_MAIN];
}

- (void)CallBackActionOpenOver:(id)Sender
{
	int i;
	CCSequence *_Seq;
	id _ActionFadeIn;
	CCCallFuncN *_CallSelectOver;

	[[CCDirector sharedDirector] setProjection:CCDirectorProjection3D];

	bookTable_[selectBookIndex_].active_page_index = 0;

	[bookTable_[selectBookIndex_].image runAction:[CCHide action]];
	for (i = 1; i < bookTable_[selectBookIndex_].pages.size(); i++)
	{
		[bookTable_[selectBookIndex_].pages[i].image runAction:[CCHide action]];
	}

	[bookTable_[selectBookIndex_].pages[0].image runAction:[CCShow action]];
	_ActionFadeIn = [CCFadeIn actionWithDuration:TIME_FADE_IN];
	_CallSelectOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionSelectOver:)];
	_Seq = [CCSequence actions:_ActionFadeIn, _CallSelectOver, nil];
	[bookTable_[selectBookIndex_].pages[0].dummy runAction:_Seq];
	for (i = 0; i < bookTable_[selectBookIndex_].pages[0].label.size(); i++)
	{
		_ActionFadeIn = [CCFadeIn actionWithDuration:0.5];
		[bookTable_[selectBookIndex_].pages[0].label[i] runAction:_ActionFadeIn];
	}
}

- (void)CallBackActionSelectOver:(id)Sender
{
	state_ = CHAPTER_STATE_PAGE;
	if (!flagSelectBookPage_)
	{
		flagTouchLock_ = false;
	}
	else
	{
		flagSelectBookPage_ = false;
		flagTurnToPage_ = true;
		[self ActionTurnToPage:turnToPageIndex_];
	}
}

- (void)CallBackActionPageFadeOutOver:(id)Sender
{
	CCSequence *_Seq;
	id _ActionCloseFront;
	id _ActionCloseNext;
	CCCallFuncN *_CallBackColseOver;

	[bookTable_[selectBookIndex_].pages[bookTable_[selectBookIndex_].active_page_index].image runAction:[CCHide action]];

	[bookTable_[selectBookIndex_].image runAction:[CCShow action]];
	_ActionCloseFront = [[CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgMenuChapterBookTable[selectBookIndex_].ani_open)] reverse];
	_ActionCloseNext = [[CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgMenuChapterBookOpenNextAni)] reverse];
	_CallBackColseOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionCloseOver:)];
	_Seq = [CCSequence actions:_ActionCloseNext, _ActionCloseFront, _CallBackColseOver, nil];
	[bookTable_[selectBookIndex_].image runAction:_Seq];
}

- (void)CallBackActionCloseOver:(id)Sender
{
	int i;
	CCSequence *_Seq;
	id _ActionMove;
	CCCallFuncN *_CallBsckUnselectOver;

	_ActionMove = [action_move_in_pixel actionWithDuration:TIME_MOVE position:bookTable_[selectBookIndex_].book_position];
	_CallBsckUnselectOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionUnselectOver:)];
	_Seq = [CCSequence actions:_ActionMove, _CallBsckUnselectOver, nil];
	[bookTable_[selectBookIndex_].image runAction:_Seq];

	for (i = 0; i < bookTable_.size(); ++i)
	{
		if (i != selectBookIndex_)
		{
			[bookTable_[i].image runAction:[CCShow action]];
			[bookTable_[i].image runAction:[CCFadeIn actionWithDuration:1.0f]];
		}
	}

	/*show item back
	*/
	flagFunctionLock_ = false;
	[itemBack_ runAction:[action_move_in_pixel actionWithDuration:BTN_MOVE_TIME position:ccp(g_ScrrenSize.width, 0.0f)]];
}

- (void)CallBackActionUnselectOver:(id)Sender
{
	[[CCDirector sharedDirector] setProjection:CCDirectorProjection2D];
	state_ = CHAPTER_STATE_BOOK;
	flagTouchLock_ = false;
	selectBookIndex_ = -1;
}

- (void)CallBackActionNextPageOver:(id)Sender
{
	CCSprite *_Cur;

	flagTouchLock_ = false;
	_Cur = bookTable_[selectBookIndex_].pages[bookTable_[selectBookIndex_].active_page_index].image;
	[_Cur runAction:[CCHide action]];
	bookTable_[selectBookIndex_].active_page_index++;

	/*check turn to page
	*/
	if (flagTurnToPage_)
	{
		if (turnToPageIndex_ == bookTable_[selectBookIndex_].active_page_index)
		{
			if (g_FlagCheckRate)
			{
				g_FlagCheckRate = false;
				if (g_RecordRate)
				{
					g_RecordRate = false;
					RecordSave();
#if (HARD_WARE == HARD_WARE_IPHONE)
					[[ReviewManager sharedReview] reviewFor:543118439];
#elif (HARD_WARE == HARD_WARE_IPAD)
					[[ReviewManager sharedReview] reviewFor:545643031];
#endif
				}
			}

			flagTouchLock_ = false;
			flagTurnToPage_ = false;
			turnToPageIndex_ = -1;
		}
		else
		{
			[self ActionNextPage];
		}
	}
}

- (void)CallBackActionPrevPageOver:(id)Sender
{
	CCSprite *_Cur;

	flagTouchLock_ = false;
	_Cur = bookTable_[selectBookIndex_].pages[bookTable_[selectBookIndex_].active_page_index].image;
	[_Cur runAction:[CCHide action]];
	bookTable_[selectBookIndex_].active_page_index--;
}

@end